<style>

</style>


<template>
  <div>
    <el-dialog 
      title="更改状态" 
      :close-on-click-modal=false
      :before-close=closeSelf
      :visible.sync="dialogFormVisible" >
      <el-form :model="dialogForm">
        <el-form-item label="产品名称" :label-width="formLabelWidth">
          <el-input v-model="dialogForm.productName" :disabled="true" auto-complete="off"></el-input>
        </el-form-item>

        <el-form-item label="上下架状态" :label-width="formLabelWidth">
          <el-select v-model="publishStatus" placeholder="请选择活动区域">
            <el-option :key='item.status' :label="item.name" :value="item.status" :disabled="item.disabled" v-for='item in showPublishStatusArr'></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="是否显示发行方" :label-width="formLabelWidth">
          <el-radio v-model="dialogForm.show_issuer" label="YES">是</el-radio>
          <el-radio v-model="dialogForm.show_issuer" label="NO">否</el-radio>
        </el-form-item>

        <el-form-item label="发布终端" :label-width="formLabelWidth">
          <el-checkbox v-model="dialogForm.publish_HCT">华财通</el-checkbox>
          <el-checkbox v-model="dialogForm.publish_LCS">理财师</el-checkbox>
          <el-checkbox v-model="dialogForm.publish_PC">PC端</el-checkbox>
        </el-form-item>

        <el-form-item label="推荐上首页" :label-width="formLabelWidth">
          <el-checkbox v-model="dialogForm.publish_HCT_top" :disabled='!dialogForm.publish_HCT'>华财通</el-checkbox>
          <el-checkbox v-model="dialogForm.publish_LCS_top" :disabled='!dialogForm.publish_LCS'>理财师</el-checkbox>
          <el-checkbox v-model="dialogForm.publish_PC_top" :disabled='!dialogForm.publish_PC'>PC端</el-checkbox>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="closeSelf">取 消</el-button>
        <el-button type="primary" @click="sumbit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
 
</template>


<script>
  import { publishStatusArr} from '../../config.js'
  import {postLog} from 'api/common'
  import {postPPStatus} from 'api/privatePlacement/index.js'

  export default {
    props:['dialogFormVisible','dialogInfo'],
    data(){
      return{
        publishStatus:'',//如果直接绑定dialogForm上的publishStatus，会改变原数据的值，
        dialogForm:{
          productName: '',
          publishStatus: '',
          show_issuer:'',
          publish_HCT:'', //华财通发布
          publish_LCS:'', //理财师发布 
          publish_PC:'', //PC发布 
          publish_HCT_top:'', //华财通首页展示
          publish_LCS_top:'',//理财师首页展示 
          publish_PC_top:'', //PC首页展示 
        },
        formLabelWidth:'120px',
        publishStatusArr:this.publishStatusArr,
      }
    },
    computed:{
      showPublishStatusArr(){
        //状态有：未上架 已上架 已下架 3步，都不能选着之前的状态
        let defaultArr = publishStatusArr;
        if(this.dialogForm.publishStatus == 'STARTING'){
          for(let i =0;i<defaultArr.length;i++){
            defaultArr[i].disabled = defaultArr[i].status == 'NOT_YET' ? true : false;
          }
        }else if(this.dialogForm.publishStatus == "END"){
          for(let i =0;i<defaultArr.length;i++){
            defaultArr[i].disabled = defaultArr[i].status != 'END' ?true :false;
          }
        }else {
          for(let i =0;i<defaultArr.length;i++){
            defaultArr[i].disabled = false
          }
        }
        return defaultArr
      },
    },
    watch:{
      dialogInfo:{
        deep:true,
        handler:function(val, oldV){
          if (!val.publish_HCT){
            val.publish_HCT_top = false
          }
          if (!val.publish_LCS){
            val.publish_LCS_top = false
          }
          if (!val.publish_PC){
            val.publish_PC_top = false
          }
        }
      }
    },
    methods:{
      closeSelf(){
        this.$emit('handleDialog')
      },
      yesToTrue(obj){
        //多选框后台的值为 YES NO
        // console.log('---yesToTrue--', JSON.stringify(obj))

        obj.publish_HCT = obj.publish_HCT && obj.publish_HCT =='YES' ? true:false; 
        obj.publish_LCS = obj.publish_LCS && obj.publish_LCS =='YES' ? true:false; 
        obj.publish_PC = obj.publish_PC && obj.publish_PC =='YES' ? true:false; 

        obj.publish_HCT_top = obj.publish_HCT_top && obj.publish_HCT_top =='YES' ? true:false; 
        obj.publish_LCS_top = obj.publish_LCS_top && obj.publish_LCS_top =='YES' ? true:false; 
        obj.publish_PC_top = obj.publish_PC_top && obj.publish_PC_top =='YES' ? true:false; 

        return obj
      },
      trueToYes(obj){
        //多选框后台的值为 YES NO
        // console.log('---trueToYes--', JSON.stringify(obj))
        let params =Object.assign({}, obj);
        params.productId = obj.id;

        params.publish_HCT = obj.publish_HCT  ? 'YES':'NO'; 
        params.publish_LCS = obj.publish_LCS ? 'YES':'NO'; 
        params.publish_PC = obj.publish_PC ? 'YES':'NO'; 

        params.publish_HCT_top = obj.publish_HCT_top ? 'YES':'NO'; 
        params.publish_LCS_top = obj.publish_LCS_top ? 'YES':'NO';  
        params.publish_PC_top = obj.publish_PC_top ? 'YES':'NO'; 
        return params
      },
      async sumbit(){
        console.log('sumbit')
        this.dialogForm.publishStatus = this.publishStatus
        try{
          let back = await postPPStatus(this.trueToYes(this.dialogForm))
          back = back.data;
          console.log('postPPStatus', this.dialogForm, back)
          if(back && back.resultCode =='200'){
            this.$alert(back.msg)
            this.$emit('reloadPage')
            postLog('PRIVATE_PLACEMENT_LIST_CHANGE_STATUS', this.dialogForm.id, this.dialogForm.productName, this.dialogForm)


          }else{
            this.$alert(back.msg)
          }
          
        }catch(err){
          console.error(err)
        }
      },
        
    },
    created(){
      this.publishStatus = this.dialogInfo.publishStatus
      this.dialogForm = this.yesToTrue(this.dialogInfo)
      
    },
    destroyed(){
    }
    
  }
</script>
